var app = getApp();
import Notify from 'vant-weapp/dist/notify/notify';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    show2: false,
    auData: {},
    imgUrls: [],
    imgUrls2: [],
    payList: [],
    goodsid: '',
    unitmoney: '',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    downTime: '',
    downTimeStr: '',
    submit: '我要出价',
    isbzj: '',
    goodsid:''
  },
  gomain() {
    wx.switchTab({
      url: '/pages/main/main',
    })
  },
  onChangenum(e) {
    console.log(e.detail);
    this.setData({
      unitmoney: e.detail
    })
  },

  onShareAppMessage: function (res) {
    console.log(res);
    var that = this;
    if (res.from === 'button') {
    }
    return {
      title: '大易益·传统文化大舞台，爱好者的掌中宝！',
      path: 'pages/audetail/audetail?goodsid='+that.data.goodsid,
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
  onClickchujia() {
    var that = this;
    if (that.data.isbzj == 'false') {
      wx.showModal({
        title: '押金提示',
        content: '参与竞拍，需先缴纳保证金？',
        success: function (sm) {
          if (sm.confirm) {
            wx.request({
              url: app.serverUrl + 'About/chongzhi',
              method: 'GET',
              dataType: 'json',
              data: {
                openid: app.openid,
              },
              success: function (res) {
                console.log(res);
                wx.requestPayment({
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,
                  'package': res.data.package,
                  'signType': 'MD5',
                  'paySign': res.data.paySign,
                  complete: function (res) {
                    // console.log(res)
                    if (res.errMsg == 'requestPayment:fail cancel' || res.errMsg == 'requestPayment:ok') {
                      that.onLoad();
                    } else {
                      wx.showToast({
                        image: '/image/icon_ca.png',
                        title: '支付失败',
                      })
                    }
                  }
                })
                wx.hideLoading()
              }
            });
          } else if (sm.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      setTimeout(() => {
        this.setData({
          show: true
        });
      }, 50);
    }

  },
  showxy(){
    console.log('aaa');
    setTimeout(() => {
      this.setData({
        show2: true
      });
    }, 50);
  },
  onClose(event) {

    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (that.data.goodsid == '') {
      this.setData({
        goodsid: options.goodsid
      })
    }
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: app.serverUrl + 'About/goodsDetail',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        goodsid: that.data.goodsid,
        paimai: 'p'
      },
      success: function (res) {

        console.log(res);
        that.setData({
          auData: res.data.data,
          imgUrls: res.data.data.goodsbanner,
          imgUrls2: res.data.data.goodsimages,
          unitmoney: res.data.data.goodsnowmoney,
          downTime: res.data.data.end,
          isbzj: res.data.data.margin,
          goodsid:res.data.data.goodsid
        });
        wx.hideLoading()
      }

    });

    wx.request({
      url: app.serverUrl + 'About/goodsBidList',
      // header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        goodsid: that.data.goodsid,
      },
      success: function (res) {

        console.log(res);
        that.setData({
          payList: res.data.data
        });
      }

    })
  },
  confirmcj(e) {
    wx.showLoading({
      title: '出价中...',
    })
    var that = this;
    wx.request({
      url: app.serverUrl + 'About/baygoodsbig',
      method: 'GET',
      dataType: 'json',
      data: {
        openid: app.openid,
        goodsid: e.currentTarget.dataset.goodsid,
        price: e.currentTarget.dataset.price,
      },
      success: function (res) {
        Notify({
          text: res.data.message,
          duration: 5000,
          selector: '#custom-selector',
          backgroundColor: '#1989fa'
        });
        that.onLoad();
        that.onClose();
        wx.hideLoading()
      },
      fail: function (res) {
        Notify({
          text: res.data.message,
          duration: 5000,
          selector: '#custom-selector',
          backgroundColor: '#f44'
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var self = this;
    console.log(self);

    var myTime = setInterval(function () {
      self.setData({
        downTime: self.data.downTime - 1

      })
      // var day = parseInt(self.data.downTime / 1000 / 60 / 60 / 24);
      // var hour = parseInt(self.data.downTime / 1000 / 60 / 60 % 24);
      // var minute = parseInt(self.data.downTime / 1000 / 60 % 60);
      // var seconds = parseInt(self.data.downTime / 1000 % 60);
      var day = Math.floor((self.data.downTime) / 84600);
      var hour = Math.floor(((self.data.downTime) - day * 84600) / 3600);
      var minute = Math.floor((self.data.downTime - day * 84600 - hour * 3600) / 60);
      var second = Math.floor(self.data.downTime - day * 84600 - hour * 3600 - minute * 60);
      if (self.data.downTime <= 0) {
        self.setData({
          downTimeStr: '00时00分00秒',
          submit: '拍卖已结束'
        })
        clearInterval(myTime);
        wx.navigateTo({
          url: '/pages/auction/auction',
        })

      } else {
        self.setData({
          downTimeStr: day + '天' + hour + '时' + minute + '分' + second + '秒'
        })
      }
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
})